package com.gen.utils.ngen;

import com.gen.utils.HttpUtils;
import com.gen.utils.lan.jp.RomajiHenkan;
import org.yaml.snakeyaml.Yaml;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ARABNameGenUtils {
    private static LinkedHashMap nameinfo;
    private static  List<List<String>> familyNames;
    private static  List<String> femaleNames;
    private static  List<String> maleNames;


    static {
        try{
            Yaml yaml = new Yaml();
            nameinfo = yaml.load(ARABNameGenUtils.class.getClassLoader()
                    .getResourceAsStream("abab_names.yml"));
//            familyNames = (List<List<String>>) nameinfo.get("last_name");

            femaleNames = (List<String>) ((LinkedHashMap) nameinfo.get("first_name")).get("female");
            maleNames = (List<String>) ((LinkedHashMap) nameinfo.get("first_name")).get("male");
        }catch (Exception e){
            e.printStackTrace();
        }


    }
    public static String genFamilyName(){
        return maleNames.get(new Random().nextInt(maleNames.size()));
    }

    public static String genName(int sex){

            if(sex ==0 ){
                return femaleNames.get(new Random().nextInt(femaleNames.size()));
            }else {
                return maleNames.get(new Random().nextInt(maleNames.size()));
            }

    }
    public static String getInsNickname(int sex){
        String nicename = "";
        int r = new Random().nextInt(100);
        if(r<5){
            String name = genFullName(sex);
            String engname = roma(name);
            nicename = engname + (new Random().nextInt(100)<50?" ":"|") + name;

        } else if(r<35){ // 全名
            nicename =  genFullName(sex);
        }else if(r<50){// 名
            nicename = genName(sex);
        }else if(r<80){// 全民名字罗马化
            nicename = roma(genFullName(sex));
        }else if(r<90){// 名字罗马化
            nicename = roma(genName(sex));
        }else {
            nicename =  genFullName(sex);
        }

        String num = "";
        if(new Random().nextInt(100)<5){
            int ageRand = new Random().nextInt(100);
            if(ageRand<100){
                num = (1988+new Random().nextInt(15))+"";
            }
        }
        nicename = nicename + num;


        if(new Random().nextInt(100)<5){
            nicename = nicename + DynamicText.getRandEmoji();
        }
        if(new Random().nextInt(100)<10){
            nicename = NameGenUtils.toRandomFont(nicename);
        }
        return  nicename.trim();
    }


    public static String getFBNickName(int sex){
        return getFBNickName(sex,false);
    }

    public static String getFBNickName(int sex,boolean useEn){
        int r = new Random().nextInt(100);
        String familyName = genFamilyName() ;

        if(r<50 || useEn){
            RomajiHenkan romajiHenkan = new RomajiHenkan();
            familyName =  romajiHenkan.convert(familyName,false) ;
            String name = genName(sex);//部分字符无法转，取可以转的
            name = romajiHenkan.convert(name,false) ;
            int namelen = name.split(" ").length;
            if(namelen>=2){
                if(name.split(" ")[0].length()>1){
                    name = name.split(" ")[0];
                }else {
                    name = name.replaceAll(" ","");
                }
            }

            r = new Random().nextInt(100);
            if(r < 50){
                return name +" "+familyName ;
            }else {
                return familyName + " " +name;

            }
        }else {
            String name = genName(sex);
            return  familyName + " " +name;
        }
    }
    public static String roma(String name)  {
        try {
            return normalizeCharacter(HttpUtils.GET("http://61.128.146.68:39999/romanize?txt="+URLEncoder.encode(name,"utf8")));
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    private static String normalizeCharacter(String curr) {
        String sdiac = "āáäĀčďḍéěḥḤíīĺľĪňóôőöŕšṣťúůűüūýřžÁÄČĎÉĚÍĹĽŇÓÔŐÖŔŠṢŤṬṭÚŮŰÜÝŘŽẒẓ";
        String bdiac = "aaaAcddeehHiillInoooorsstuuuuuyrzAACDEEILLNOOOORSSTTtUUUUYRZZz";
        char[] s = sdiac.toCharArray();
        char[] b = bdiac.toCharArray();
        for(int i = 0; i < sdiac.length(); i++){
            curr = curr.replaceAll(s[i]+"",b[i]+"");
        }

        return curr;
    }

    public static String genFullName(int sex){
        return genFullName(sex," ");
    }
    public static String genFullName(int sex,String split){
        String familyname = genFamilyName();
        String name = genName(sex);

        return name + split + familyname;
    }

    public static void main(String[] arg ) throws IOException {
        Pattern p = Pattern.compile("[\u0600-\u06ff\u0750-\u077f\ufb50-\ufc3f\ufe70-\ufefc]");
        Matcher matcher = p.matcher("来访");
        System.out.println(matcher.find());

        for (int i = 0;i<200;i++ ) {
            String nick = getInsNickname(1);
            System.out.println(nick+"--------"+InsUsernameGen.genUsernameByNickname(nick,1,""));

//            String name = genFullName(1);
//
//            System.out.println(name+"|"+roma(name));
//            System.out.println(""+roma(name)+"|"+name);


//             String cname = romajiHenkan.convert(name,true);

//             System.out.println(name+" "+cname);
        }

    }
}
